<template>
  <div class="app-container">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>招聘会管理</span>
      </div>
      <div class="list-search">
        <el-select
          v-model="status"
          placeholder="预定状态"
          class="list-options"
          @change="funSearch"
        >
          <el-option label="不限预定状态" value="" />
          <el-option label="未开始预定" value="0" />
          <el-option label="永许预定" value="1" />
          <el-option label="停止预定" value="2" />
        </el-select>
        <el-select
          v-model="settr"
          placeholder="添加时间"
          class="list-options"
          @change="funSearch"
        >
          <el-option label="不限添加时间" value="" />
          <el-option label="三天内" value="3" />
          <el-option label="一周内" value="7" />
          <el-option label="一月内" value="30" />
          <el-option label="半年内" value="180" />
          <el-option label="一年内" value="360" />
        </el-select>
        <el-input
          v-model="keyword"
          placeholder="请输入搜索内容"
          class="input-with-select"
          @keyup.enter.native="funSearchKeyword"
        >
          <el-select
            slot="prepend"
            v-model="key_type"
            placeholder="请选择"
            class="input-sel"
          >
            <el-option label="标题" value="1" />
          </el-select>
          <el-button
            slot="append"
            icon="el-icon-search"
            @click="funSearchKeyword"
          />
        </el-input>
      </div>
      <div class="spaceline" />
      <el-table
        v-loading="listLoading"
        :data="list"
        element-loading-text="Loading"
        fit
        highlight-current-row
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="42" />
        <el-table-column label="举办时间" show-overflow-tooltip width="260">
          <template slot-scope="scope">
            {{ scope.row.holddate_start|timeFilter }} 至 {{ scope.row.holddate_end|timeFilter }}
          </template>
        </el-table-column>
        <el-table-column label="标题" show-overflow-tooltip min-width="150">
          <template slot-scope="scope">
            <el-link :href="scope.row.jobfair_link" target="_blank" type="primary">
              {{ scope.row.title }}
            </el-link>
          </template>
        </el-table-column>
        <el-table-column label="预定状态" width="100">
          <template slot-scope="scope">
            <span v-if="scope.row.predetermined == 0" style="color:#e6a23c;">{{ scope.row.predetermined_text }}</span>
            <span v-if="scope.row.predetermined == 1" style="color:#67c23a;">{{ scope.row.predetermined_text }}</span>
            <span v-if="scope.row.predetermined == 2" style="color:#f56c6c;">{{ scope.row.predetermined_text }}</span>
          </template>
        </el-table-column>
        <el-table-column label="下载二维码" align="center" width="90">
          <template slot-scope="scope">
            <img class="qrcode" :src="require('@/assets/images/qrcod.png')" @click="showQrcode(scope.row)">
          </template>
        </el-table-column>
        <el-table-column label="显示状态" width="100">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.display"
              @change="modifyState(scope.row)">
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="created_at"
          label="添加日期"
          width="150"
        >
          <template slot-scope="scope">
            <i class="el-icon-time" />
            <span>{{ scope.row.addtime | timeFilter }}</span>
          </template>
        </el-table-column>
        <el-table-column fixed="right" align="right" label="操作" width="420">
          <template slot-scope="scope">
            <el-button
              v-if="scope.row.holddate==2"
              size="mini"
              type="primary"
              @click="funRetrospect(scope.$index, scope.row)"
            >
              精彩回顾
            </el-button>
            <el-button
              size="mini"
              type="primary"
              @click="funPosition(scope.$index, scope.row)"
            >
              展位设置
            </el-button>
            <el-button
              size="mini"
              type="primary"
              @click="funCompany(scope.$index, scope.row)"
            >
              参会企业
            </el-button>
            <el-button
              size="mini"
              type="primary"
              @click="funEdit(scope.$index, scope.row)"
            >
              编辑
            </el-button>
            <el-button
              size="mini"
              type="danger"
              @click="funDelete(scope.$index, scope.row)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="spaceline" />
      <el-row :gutter="20">
        <el-col :span="8">
          <el-button size="small" type="primary" @click="goAdd">
            添加招聘会
          </el-button>
          <el-button size="small" type="danger" @click="funDeleteJobfair">
            删除所选
          </el-button>
        </el-col>
        <el-col :span="16" style="text-align: right;">
          <el-pagination
            background
            :current-page="currentPage"
            :page-sizes="[10, 20, 50, 100]"
            :page-size="pagesize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </el-col>
      </el-row>
      <el-dialog
        v-if="showQrcodeWindow"
        title="下载二维码"
        :visible.sync="showQrcodeWindow"
        width="50%"
        :close-on-click-modal="false"
        @close="showQrcodeWindow=false"
      >
        <qrcode :jobfair-id="jobfairId" @setDialogFormVisible="showQrcodeWindow=false" />
      </el-dialog>
    </el-card>
  </div>
</template>
<script>
import qrcode from './qrcode.vue'
import { jobfairList, jobfairDelete, jobfairModifyState } from '@/api/jobfair'
import { parseTime } from '@/utils/index'
export default {
  components: {
    qrcode
  },
  filters: {
    timeFilter(timestamp) {
      return parseTime(timestamp, '{y}-{m}-{d} {h}:{i}')
    },
    displayFilter(display) {
      const displayMap = {
        1: 'success',
        0: 'danger'
      }
      return displayMap[display]
    },
    predeterminedFilter(predetermined) {
      switch (predetermined) {
        case 0:
          return 'info'
        case 1:
          return 'success'
        case 2:
          return 'danger'
        default:
          return ''
      }
    }
  },
  data() {
    return {
      status: '',
      settr: '',
      keyword: '',
      key_type: '1',
      total: 0,
      currentPage: 1,
      pagesize: 10,
      listLoading: !1,
      list: [],
      tableIdarr: [],
      showQrcodeWindow: !1,
      jobfairId: 0
    }
  },
  created() {
    this.getJobfair()
  },
  methods: {
    modifyState(row){
      const that = this
      const param = {
        display: row.display,
        id: row.id
      }
      jobfairModifyState(param).then(response => {
        this.$message.success(response.message)
        setTimeout(function() {
          that.$router.push('/jobfair/list')
        }, 1500)
        return true
      }).catch(() => {
        that.issubmit = false
      })
    },
    getJobfair: function(){
      this.listLoading = true
      const param = {
        status: this.status,
        settr: this.settr,
        key_type: this.key_type,
        keyword: this.keyword,
        page: this.currentPage,
        pagesize: this.pagesize
      }
      jobfairList(param).then(response => {
        this.list = response.data.items
        this.listLoading = false
        this.total = response.data.total
        this.currentPage = response.data.current_page
        this.pagesize = response.data.pagesize
      })
    },
    funSearch: function(){
      this.getJobfair()
    },
    funSearchKeyword: function(){
      this.currentPage = 1
      this.getJobfair()
    },
    funRetrospect: function(index, row){
      this.$router.push({
        path: '/jobfair/retrospect',
        query: {
          id: row.id
        }
      })
    },
    funPosition: function(index, row) {
      this.$router.push({
        path: '/jobfair/jobfair_position',
        query: {
          id: row.id
        }
      })
    },
    funCompany: function(index, row){
      this.$router.push({
        path: '/jobfair/exhibitors/list',
        query: {
          id: row.id
        }
      })
    },
    funEdit: function(index, row){
      this.$router.push({
        path: '/jobfair/edit',
        query: {
          id: row.id
        }
      })
    },
    funDelete: function(index, row){
      var that = this
      that
        .$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        .then(() => {
          const param = {
            id: row.id
          }
          jobfairDelete(param).then(response => {
            that.$message.success(response.message)
            that.getJobfair()
            return true
          })
        })
        .catch(() => {})
    },
    funDeleteJobfair: function(){
      var that = this
      if (that.tableIdarr.length == 0) {
        that.$message.error('请选择要删除的数据')
        return false
      }
      that
        .$confirm('此操作将永久删除选中的数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        .then(() => {
          const param = {
            id: that.tableIdarr
          }
          jobfairDelete(param).then(response => {
            that.$message.success(response.message)
            that.getJobfair()
            return true
          })
        })
        .catch(() => {})
    },
    handleSizeChange: function(val){
      this.pagesize = val
      this.getJobfair()
    },
    handleCurrentChange: function(val){
      this.currentPage = val
      this.getJobfair()
    },
    goAdd: function(){
      this.$router.push('/jobfair/add')
    },
    handleSelectionChange(idlist) {
      this.tableIdarr = []
      if (idlist.length > 0) {
        for (const item of idlist) {
          this.tableIdarr.push(item.id)
        }
      }
    },
    showQrcode: function(row){
      this.jobfairId = row.id
      this.showQrcodeWindow = !0
    }
  }
}
</script>
<style lang="scss" scoped>
  .qrcode{vertical-align:middle}
</style>
